package slidingpenguins.utils.imagecache {
	import flash.errors.IOError;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.utils.ByteArray;
	

	[Bindable]
	public class CachedImageSummary {
		private var _state:String;
		private var _percantageComplete:uint;
		private var _bytes:ByteArray;

		public function CachedImageSummary(image:CachedImage) {
			percantageComplete = 0;
			bytes = null;

			if (image.isReady) {
				state = "readyState";
				bytes = image.bytes;
			} else if (image.isInErrorState) {
				_state = "errorState";
			} else {
				state = "progressState";
				
				image.addEventListener(Event.COMPLETE, function(event:Event):void {
					state = "readyState";
					bytes = image.bytes;
				});
				image.addEventListener(ProgressEvent.PROGRESS, function(event:ProgressEvent):void {
					state = "progressState";
					percantageComplete = (event.bytesLoaded / event.bytesTotal) * 100;
				});
				image.addEventListener(ErrorEvent.ERROR, function(event:IOError):void {
					state = "errorState";
				});
			}
		}
		
		public function get bytes():ByteArray {
			return _bytes;
		}
		
		public function set bytes(bytes:ByteArray):void {
			_bytes = bytes;
		}
		
		public function get state():String {
			return _state;
		}
		
		public function set state(state:String):void {
			_state = state;
		}
		
		public function get percantageComplete():uint {
			return _percantageComplete;
		}
		
		public function set percantageComplete(percantageComplete:uint):void {
			_percantageComplete = percantageComplete;
		}
	}
}
